﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using TPLOAWebClient.Resources;
using System.Web.Security;

namespace TPLOAWebClient
{
    public partial class clinicprofile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpClient client = (HttpClient)Session["httpclient"];
            HttpResponseMessage response = null;

            if (Session["username"] != null)
            {
                response = client.GetAsync("api/members/" + Session["username"]).Result;

                var member = response.Content.ReadAsAsync<Member>().Result;
                if (member.UserType.Equals("Student"))
                {
                    Response.Redirect("studentprofile.aspx");
                }
                else if (member.UserType.Equals("Admin"))
                {
                    Response.Redirect("adminprofile.aspx");
                }
                else if (member.UserType.Equals("Staff"))
                {
                    Response.Redirect("staffprofile.aspx");
                }
            }

            if (!Page.IsPostBack)
            {
                response = client.GetAsync("api/Clinics/" + Session["username"]).Result;  // Blocking call!
                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body. Blocking!
                    var clinic = response.Content.ReadAsAsync<Clinic>().Result;
                    clinicidLbl.Text = clinic.ClinicID;
                    nameTbx.Text = clinic.Name;
                    addressTbx.Text = clinic.Address;
                    telnoTbx.Text = clinic.TelNo;
                }
                else
                {
                    retrieveLbl.Text += response.StatusCode + "(" + response.ReasonPhrase + ")";
                }
            }
        }
        protected void profileBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("clinicprofile.aspx");
        }

        protected void submitloaBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("clinicsubmitloa.aspx");
        }

        protected void faqBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("clinicfaq.aspx");
        }

        protected void otherservicesBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("clinicotherservice.aspx");
        }

        protected void logoutBtn_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("default.aspx");
        }

        protected void updateBtn_Click(object sender, EventArgs e)
        {
            HttpClient client = (HttpClient)Session["httpclient"];

            var clinic = new Clinic()
            {
                ClinicID = clinicidLbl.Text,
                Name = nameTbx.Text,
                Address = addressTbx.Text,
                TelNo = telnoTbx.Text
            };
            HttpResponseMessage response = client.PutAsJsonAsync("api/Clinics/" + clinicidLbl.Text, clinic).Result;  // Blocking call!
            if (response.IsSuccessStatusCode)
            {
                retrieveLbl.Visible = true;
                retrieveLbl.Text = "Clinic Updated";
            }
            else
            {
                retrieveLbl.Visible = true;
                retrieveLbl.Text = response.StatusCode + "(" + response.ReasonPhrase + ")";
            }
        }

        protected void unregisterBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                HttpClient client = (HttpClient)Session["httpclient"];

                HttpResponseMessage response = client.DeleteAsync("api/Clinics/" + clinicidLbl.Text).Result;
                HttpResponseMessage response2 = client.DeleteAsync("api/Members/" + clinicidLbl.Text).Result;
                if (response.IsSuccessStatusCode && response2.IsSuccessStatusCode)
                {
                    Session.Abandon();
                    FormsAuthentication.SignOut();
                    Response.Redirect("default.aspx");
                }
                else
                {
                    Response.Redirect("clinicprofile.aspx");
                }
            }
        }
    }
}